/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hijh.uniandes.ecos.runt.persistence;

import javax.persistence.Query;

/**
 *
 * @author Usuario
 */
public class RNLOperation {
        
    protected RNLOperation(){
    }

    public static int AddRecord(hijh.uniandes.ecos.runt.persistence.RNLRecord record)
    {
        int succeededcount = 0;
        javax.persistence.EntityManager em = null;
        try{
            em = hijh.uniandes.ecos.runt.persistence.RNLJPAManager.getEMF().createEntityManager();
            javax.persistence.EntityTransaction etx = em.getTransaction();
            etx.begin();
            if(em.find(RNLRecord.class, record.getId())==null)
                em.persist(record);
            else
                em.merge(record);
            etx.commit();
        }finally{
            if(em!=null)
                em.close();
        }
        return succeededcount;
    }

    public static int UpdateRecord(hijh.uniandes.ecos.runt.persistence.RNLRecord record)
    {
        int succeededcount = 0;
        javax.persistence.EntityManager em = null;
        try{
            em = hijh.uniandes.ecos.runt.persistence.RNLJPAManager.getEMF().createEntityManager();
            Query query = em.createNamedQuery("updaternlrecord");
            query.setParameter("RECORD", record);
            succeededcount = query.executeUpdate();
        }finally{
            if(em!=null)
                em.close();
        }
        return succeededcount;
    }

    public static int RemoveRecord(hijh.uniandes.ecos.runt.persistence.RNLRecord record)
    {
        int succeededcount = 0;
        javax.persistence.EntityManager em = null;
        try{
            em = hijh.uniandes.ecos.runt.persistence.RNLJPAManager.getEMF().createEntityManager();
            javax.persistence.EntityTransaction etx = em.getTransaction();
            etx.begin();
            em.remove(record);
            etx.commit();
        }finally{
            if(em!=null)
                em.close();
        }
        return succeededcount;
    }

    public static int RemoveRecordById(String uuid)
    {
        int succeededcount = 0;
        javax.persistence.EntityManager em = null;
        try{
            em = hijh.uniandes.ecos.runt.persistence.RNLJPAManager.getEMF().createEntityManager();
            javax.persistence.EntityTransaction etx = em.getTransaction();
            etx.begin();
            RNLRecord record = em.find(RNLRecord.class, uuid);
            em.remove(record);
            etx.commit();
        }finally{
            if(em!=null)
                em.close();
        }
        return succeededcount;
    }
    
    public static java.util.Collection<hijh.uniandes.ecos.runt.persistence.RNLRecord> GetAllRecords()
    {
        javax.persistence.EntityManager em = null;
        java.util.Collection<hijh.uniandes.ecos.runt.persistence.RNLRecord> records = null;
        try{
            em = hijh.uniandes.ecos.runt.persistence.RNLJPAManager.getEMF().createEntityManager();
            Query query = em.createNamedQuery("allrnlrecords");
            records = query.getResultList();
        }finally{
            if(em!=null)
                em.close();
        }
        return records;
    }

    public static hijh.uniandes.ecos.runt.persistence.RNLRecord GetRecordById(String uuid)
    {
        javax.persistence.EntityManager em = null;
        hijh.uniandes.ecos.runt.persistence.RNLRecord record = null;
        try{
            em = hijh.uniandes.ecos.runt.persistence.RNLJPAManager.getEMF().createEntityManager();
            record = em.find(RNLRecord.class, uuid);
        }finally{
            if(em!=null)
                em.close();
        }
        return record;
    }
    
    public static hijh.uniandes.ecos.runt.persistence.RNLRecord GetRecordByKey(String key)
    {
        javax.persistence.EntityManager em = null;
        hijh.uniandes.ecos.runt.persistence.RNLRecord record = null;
        try{
            em = hijh.uniandes.ecos.runt.persistence.RNLJPAManager.getEMF().createEntityManager();
            Query query = em.createNamedQuery("rnlrecordbykey");
            query.setParameter("KEY", key);
            record = (RNLRecord)query.getSingleResult();
        }finally{
            if(em!=null)
                em.close();
        }
        return record;
    }
    
}
